﻿@model IEnumerable<InVision_Ticket.ViewModels.TicketListViewModel>

@{
    ViewBag.Title = "Tickets";
}
<button id="TicketSearchButton" onclick="ToggleSearch()"> Search For a Ticket</button>
<div class="HeaderWrapper">Tickets</div>

@using (Html.BeginForm())
{
    
    <br />
    <div id="TicketSearchDiv">
        <fieldset>
        <legend>Search</legend>
            <label>Search:</label>
            <input type="text" id="SearchText" name="SearchText"/>&nbsp;
            <label>Created After:</label>
            <input type="date" id="StartDate" name="StartDate"/> &nbsp;
            <label>Created Before:</label>
            <input type="date" id="EndDate" name="EndDate"/>&nbsp;&nbsp;
            <br />
            All:<input type="radio" name="StatusSearch" value=""  checked/>
            Open:<input type="radio" name="StatusSearch" value="True" />
            Closed:<input type="radio" name="StatusSearch" value="False" />&nbsp;&nbsp;
            My Location Only:<input type="checkbox" name="SearchLocation" value="True" checked/>
            <input type="submit" id="submit" value="Search" /> 
        </fieldset>
    </div>
}



<p>
    @Html.ActionLink("Create New Ticket", "Create")
</p>
<table class="TicketListTable sortable">
    <tr>
        <th>
            ID
        </th>
        <th>
            Summary
        </th>
        <th>
            Last Modified
        </th>
        <th>
            Store
        </th>
        <th>
            Status
        </th>
        <th>
            Phone
        </th>
        <th>
            Customer
        </th>
        <th>
            Type
        </th>
        <th>
            Created By
        </th>
        <th>
            Salesman
        </th>
        <th>
            Tech
        </th>
    </tr>

@foreach (var item in Model)
{
    <tr title=Created&nbsp;On:&nbsp;@item.CreatedDateTime.ToUniversalTime() class="@GetTRClass(item.TechnicianLoginID, item.SalesmanLoginID, item.TicketStatusAttentionRequired)" onclick="OpenTicket('@item.TicketID')">

        <td>
            @Html.DisplayFor(modelItem => item.TicketID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Summary)
        </td>
        <td>
            @if (item.LastModifiedDateTime.HasValue)
            {
                @Html.DisplayFor(modelItem => item.LastModifiedDateTime.Value)
            }
            else
            {
                @Html.DisplayFor(modelItem => item.CreatedDateTime)
            }
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Location)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TicketStatus)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CustomerPhone)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CustomerName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TicketType)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CreatedBy)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Salesman)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Technician)
        </td>

    </tr>
}

</table>
<script src="Scripts/sorttable.js"></script>
<script type="text/javascript">
    function OpenTicket(id) {
        var url = "/Ticket/Edit/" + id;
        var win = window.open(url, '_blank');
        win.focus();

    }
    function ToggleSearch() {
        var div = document.getElementById("TicketSearchDiv");
        if (div.style.display == "block") {
            $(div).slideUp(2000);
        }
        else {
            $(div).slideDown(2000);
        }
    
    }

</script>

<style type="text/css">

    .ticketListRow:hover
    {
        background-color:#B0C4DE;
    
    }
    .ticketListRow td
    {
        border-left:1px solid LightGray;
    }
    .attention
    {
        background-color:#FFA9A9;    
    }
</style>


@functions{ 

    public string GetTRClass(int? TechID, int? SalesID, bool? AttentionRequired)
    {
        if (AttentionRequired.HasValue)
        {
            if (AttentionRequired.Value)
            {
                var userID = ViewBag.userID;
                if (TechID.HasValue)
                {
                    if (TechID.Value == userID)
                    {
                        return "ticketListRow attention";
                    }

                }
                if (SalesID.HasValue)
                {
                    if (SalesID.Value == userID)
                    {
                        return "ticketListRow attention";
                    }
                }
                return "ticketListRow";
            }
            return "ticketListRow";
        }
        return "ticketListRow";
    }
    
}